package com.mccken.springframework.beans.factory.support;

import java.lang.reflect.Constructor;

import com.mccken.springframework.beans.BeansException;
import com.mccken.springframework.beans.factory.config.BeanDefinition;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.NoOp;

/**
 * @program: mccken-collect
 * @description:
 * @author: mccken
 * @create: 2022-11-28 18:50
 **/
public class CglibSubclassingInstantiationstrategy implements InstantiationStrategy {
	@Override
	public Object instantiate(BeanDefinition beanDefinition, String beanName, Constructor constructor, Object[] args) throws BeansException {
		Enhancer enhancer = new Enhancer();
		enhancer.setSuperclass(beanDefinition.getBeanClass());
		enhancer.setCallback(new NoOp() {
			@Override
			public int hashCode() {
				return super.hashCode();
			}
		});
		
		if (constructor == null) {
			return enhancer.create();
		}
		return enhancer.create(constructor.getParameterTypes(), args);
	}
}
